<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\GetBairongData;
use App\Admin\Extensions\RiskOrderExporter;
use App\RiskOrder;
use App\RiskRule;
use App\RiskRuleAli;
use App\Services\RuleService;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class RiskOrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '风控订单';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RiskOrder());

        $grid->model()->orderByDesc('id');

        $grid->disableCreateButton();
        $grid->disableColumnSelector();
//        $grid->disableRowSelector();
        $grid->expandFilter();
//        $grid->disableActions();
//        $grid->exporter(new RiskOrderExporter());
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->add( new GetBairongData());
//            $actions->disableView();
        });
        $grid->filter(function(Grid\Filter $filter){

            $filter->disableIdFilter();

            $filter->column(1/2, function(Grid\Filter $filter){
                $filter->equal('orderid', __('Orderid'));
                $filter->contains('ordersn',  __('Ordersn'));
                $filter->equal('id_card', __('Id card'));
                $filter->equal('mobile', __('Mobile'));
            });
            $filter->column(1/2, function(Grid\Filter $filter){
                $filter->equal('dengji', __('Dengji'))->select(RiskRule::DENGJI_MAP);
                $filter->equal('dengji_ali', '智租等级')->select(RiskRuleAli::LEVEL_MAP);
                $filter->equal('status', '状态')->select(RiskOrder::STATUS_MAP);
                $filter->between('created_at',__('Created at'))->datetime();
                $filter->between('xaidan_at','下单时间')->datetime();
            });
        });


        $grid->column('id', __('Id'));
        $grid->column('orderid', __('Orderid'));
        $grid->column('ordersn', __('Ordersn'));
        $grid->column('id_name', __('Id name'));
        $grid->column('id_card', __('Id card'));
        $grid->column('dengji', __('Dengji'))->using(RiskRule::DENGJI_MAP);
        $grid->column('mingzhong', '命中规则')->expand(function ($model) {
            $rule_service = new RuleService();
            $data = $rule_service->updateLevel($model);

            return new Table([ '规则名称', '条件名称'], $data['mingzhi']);
        });
        $grid->column('dengji_ali', '智租等级');
        $grid->column('rule_ali_id', '智租规则ID');

        $grid->column('status', __('Status'))->using(RiskOrder::STATUS_MAP);
//        $grid->column('risk_rules_id', __('Risk rules id'));
//        $grid->column('sys', __('Sys'));
//        $grid->column('sjzwzt', __('Sjzwzt'));
//        $grid->column('fyxxxqfjb', __('Fyxxxqfjb'));
//        $grid->column('jdyx', __('Jdyx'));
//        $grid->column('tsmd', __('Tsmd'));
//        $grid->column('fxsb', __('Fxsb'));
//        $grid->column('data', __('Data'));
        $grid->column('xaidan_at', '下单时间');
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'))->hide();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RiskOrder::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('orderid', __('Orderid'));
        $show->field('ordersn', __('Ordersn'));
        $show->field('id_name', __('Id name'));
        $show->field('id_card', __('Id card'));
        $show->field('status', __('Status'));
        $show->field('risk_rules_id', __('Risk rules id'));
        $show->field('sys', __('Sys'));
        $show->field('sjzwzt', __('Sjzwzt'));
        $show->field('fyxxxqfjb', __('Fyxxxqfjb'));
        $show->field('jdyx', __('Jdyx'));
        $show->field('tsmd', __('Tsmd'));
        $show->field('fxsb', __('Fxsb'));
        $show->field('data', __('Data'));
        $show->field('dengji_ali', '智租等级');
        $show->field('data_ali', '智租数据')->as(function ($data) {
            return json_encode(json_decode($data,true),JSON_UNESCAPED_UNICODE);
        });
        $show->field('rule_ali_id', '智租规则ID');
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RiskOrder());

        $form->number('orderid', __('Orderid'));
        $form->text('ordersn', __('Ordersn'));
        $form->text('id_name', __('Id name'));
        $form->text('id_card', __('Id card'));
        $form->number('status', __('Status'));
        $form->number('risk_rules_id', __('Risk rules id'));
        $form->text('sys', __('Sys'));
        $form->text('sjzwzt', __('Sjzwzt'));
        $form->text('fyxxxqfjb', __('Fyxxxqfjb'));
        $form->text('jdyx', __('Jdyx'));
        $form->text('tsmd', __('Tsmd'));
        $form->text('fxsb', __('Fxsb'));
        $form->textarea('data', __('Data'));

        return $form;
    }
}
